/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.xcxAdmin.web;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.service.ServiceException;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.ExcelImport;
import com.jeesite.common.utils.excel.annotation.ExcelField;
import com.jeesite.common.validator.ValidatorUtils;
import com.jeesite.modules.pay.totalPay.util.StringUtil;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.goods.entity.ZtCardMem;
import com.jeesite.modules.goods.service.ZtCardMemService;
import com.jeesite.modules.goods.service.ZtCardService;
import com.jeesite.modules.xcxAdmin.entity.ZtAdmin;
import com.jeesite.modules.xcxAdmin.service.ZtAdminService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztAdmin")
public class ZtAdminController extends BaseController {

	@Autowired
	private ZtAdminService ztAdminService;
	@Autowired
	private ZtCardMemService ztCardMemService;
	@Autowired
	private ZtCardService ztCardService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtAdmin get(String id, boolean isNewRecord) {
		return ztAdminService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("admin:adminData:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtAdmin ztAdmin, Model model) {
		model.addAttribute("ztAdmin", ztAdmin);
		return "modules/admin/ztAdminList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("admin:adminData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtAdmin> listData(ZtAdmin ztAdmin, HttpServletRequest request,
			HttpServletResponse response) {
		Page<ZtAdmin> page = ztAdminService.findPage(new Page<ZtAdmin>(request,
				response), ztAdmin);
		return page;
	}
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("admin:adminData:view")
	@RequestMapping(value = "form")
	public String form(ZtAdmin ztAdmin, Model model) {
		model.addAttribute("ztAdmin", ztAdmin);
		return "modules/admin/ztAdminForm";
	}
	/**
	 * 删除数据
	 */
	@RequiresPermissions("admin:adminData:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtAdmin ztAdmin) {
		ztAdminService.delete(ztAdmin);
		return renderResult(Global.TRUE, text("删除用户成功！"));
	}
	/**
	 * 保存数据
	 */
	@RequiresPermissions("admin:adminData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(ZtAdmin ztAdmin) {
		
		if(StringUtils.isBlank(ztAdmin.getBid())){
			Date date2 = new Date(System.currentTimeMillis());
			String str = String.format("%tj",date2);//得到time日期是在这年的第几天
			
			int s=ztAdminService.getCount()+1;
			
			String u=String.format("%06d", s);
			ztAdmin.setBid(str+u);
		}
		if("0".equals(ztAdmin.getLevel())){
			
			if(StringUtils.isBlank(ztAdmin.getOleMemType())){
				if("1".equals(ztAdmin.getMemtype())){
					saveCardAdmin(ztAdmin);
				}
			}else if(!ztAdmin.getMemtype().equals(ztAdmin.getOleMemType())){
				if("0".equals(ztAdmin.getMemtype())){
					deleteCardAdmin(ztAdmin);
				}else{
					saveCardAdmin(ztAdmin);
				}
			}
			
		}else{
			
			ztAdmin.setMemtype("1");
			saveCardAdmin(ztAdmin);
		}
		if(ztAdmin.getPhone().equals("")){
			ztAdmin.setPhone(null);
		}
		ztAdminService.setNames();
		ztAdminService.save(ztAdmin);
		
		return renderResult(Global.TRUE, text("保存用户成功！"));
		
	}
	
	
	public void deleteCardAdmin(ZtAdmin ztAdmin){
		
		ZtCardMem ztCardMem=new ZtCardMem();
		ztCardMem.setAdminid(ztAdmin.getId());
	
		ZtCardMem zm=ztCardMemService.getByAdminid(ztCardMem);
		if(zm!=null){
			ztCardMemService.delete(zm);
		}
	}
	/**
	 * 导入用户数据
	 */
	@ResponseBody
	@RequiresPermissions("sys:empUser:edit")
	@PostMapping(value = "importData")
	public String importData(MultipartFile file, String updateSupport,HttpServletResponse response) {
		try {
			boolean isUpdateSupport = Global.YES.equals(updateSupport);
			String successMsg= ztAdminService.importData(file, isUpdateSupport);

			return renderResult(Global.TRUE, "posfull:"+successMsg);

		} catch (Exception ex) {
			return renderResult(Global.FALSE, "posfull:"+ex.getMessage());
		}
	}
	/**
	 * 下载导入用户数据模板
	 */
	@RequiresPermissions("sys:empUser:view")
	@RequestMapping(value = "importTemplate")
	public void importTemplate(HttpServletResponse response) {
		ZtAdmin ztAdmin = new ZtAdmin();
		List<ZtAdmin> list = ListUtils.newArrayList(ztAdmin);
		String fileName = "用户数据模板.xlsx";
		new ExcelExport("用户数据", ZtAdmin.class, ExcelField.Type.IMPORT).setDataList(list)
				.write(response, fileName).dispose();
	}



	public void saveCardAdmin(ZtAdmin ztAdmin){
		
		ZtCardMem ztCardMem=new ZtCardMem();
		ztCardMem.setAdminid(ztAdmin.getId());
		ztCardMem.setCardid(ztCardService.getOneCard().getId());
		Date da=new Date();
		ztCardMem.setStartDate(da);
		ztCardMem.setEndDate(DateUtils.addYears(da, 1));
		ztCardMem.setCreateDate(da);
		ZtCardMem zm=ztCardMemService.getByAdminid(ztCardMem);
		if(zm!=null){
			zm.setStartDate(da);
			zm.setEndDate(DateUtils.addYears(da, 1));
			zm.setCreateDate(da);
			ztCardMemService.save(zm);
		}else{
			ztCardMemService.save(ztCardMem);
		}
	}
	
}